set(TASKFLOW_EXTERNAL_DIR "${CMAKE_BINARY_DIR}/external/taskflow")
set(TASKFLOW_BUILD_DIR "${TASKFLOW_EXTERNAL_DIR}/build")
set(TASKFLOW_DOWNLOAD_DIR "${TASKFLOW_EXTERNAL_DIR}/download")
set(TASKFLOW_INSTALL_DIR "${TASKFLOW_EXTERNAL_DIR}/install")
set(TASKFLOW_SRC_DIR "${TASKFLOW_EXTERNAL_DIR}/src")
set(TF_BUILD_BENCHMARKS OFF CACHE BOOL "" FORCE)
set(TF_BUILD_CUDA OFF CACHE BOOL "" FORCE)
set(TF_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(TF_BUILD_SYCL OFF CACHE BOOL "" FORCE)
set(TF_BUILD_TESTS OFF CACHE BOOL "" FORCE)

file(WRITE "${TASKFLOW_DOWNLOAD_DIR}/CMakeLists.txt"
  "cmake_minimum_required(VERSION ${CMAKE_MINIMUM_REQUIRED_VERSION})
  include(ExternalProject)
  project(taskflow-download CXX)
  ExternalProject_Add(taskflow-download
    GIT_REPOSITORY https://github.com/taskflow/taskflow.git
    GIT_TAG v3.7.0
    GIT_SHALLOW 1
    SOURCE_DIR \"${TASKFLOW_SRC_DIR}\"
    BINARY_DIR \"${TASKFLOW_BUILD_DIR}\"
    CONFIGURE_COMMAND \"\"
    BUILD_COMMAND \"\"
    INSTALL_COMMAND \"\"
    TEST_COMMAND \"\"
  )"
)

execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE TASKFLOW_CMAKE_RESULT
  WORKING_DIRECTORY ${TASKFLOW_DOWNLOAD_DIR}
)

if(TASKFLOW_CMAKE_RESULT)
  message(FATAL_ERROR "CMake for taskflow-download failed: ${TASKFLOW_CMAKE_RESULT}")
endif()

execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE TASKFLOW_BUILD_RESULT
  WORKING_DIRECTORY ${TASKFLOW_DOWNLOAD_DIR}
)

if(TASKFLOW_BUILD_RESULT)
  message(FATAL_ERROR "Build for taskflow-download failed: ${TASKFLOW_BUILD_RESULT}")
endif()

add_subdirectory(${TASKFLOW_SRC_DIR} ${TASKFLOW_BUILD_DIR})
